package com.ruiysoft.controller.statistics;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruiysoft.controller.statistics.vo.StatisticsListVo;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.mapper.statistics.ShopUserStatisticsMapper;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultCode;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.pub.entity.PageInfo;
import com.ruiysoft.service.statistics.IShopUserStatisticsService;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-03-25T15:30:27.857+08:00")

@RestController
public class StatisticsApiController implements StatisticsApi {

    private static final Logger log = LoggerFactory.getLogger(StatisticsApiController.class);

    private final IShopUserStatisticsService shopUserStatisticsService;

    @Autowired
    ShopUserStatisticsMapper shopUserStatisticsMapper;

    @org.springframework.beans.factory.annotation.Autowired
    public StatisticsApiController(IShopUserStatisticsService shopUserStatisticsService) {
        this.shopUserStatisticsService = shopUserStatisticsService;
    }

    private Long getShopId() {
        SysMicroShop shop = LoginContextUtil.instance().getEntryMicroShop();
        return shop.getId();
    }

    @Override
    public Result<PageInfo<StatisticsListVo>> statisticsBydayPut(@NotNull @ApiParam(value = "分页开始页", required = true) @Valid @RequestParam(value = "pageStart", required = true) Integer pageStart, @NotNull @ApiParam(value = "每页显示多少条数据", required = true) @Valid @RequestParam(value = "pageSize", required = true) Integer pageSize, @NotNull @ApiParam(value = "查询类型（1，店铺访问，2，商品访问，3，加入购物车，4下单）", required = true) @Valid @RequestParam(value = "type", required = true) Integer type, @ApiParam(value = "开始时间（时间戳）") @Valid @RequestParam(value = "sTime", required = false) Long sTime, @ApiParam(value = "结束时间（时间戳）") @Valid @RequestParam(value = "eTime", required = false) Long eTime, @ApiParam(value = "skuid") @Valid @RequestParam(value = "skuId", required = false) Long skuId, @ApiParam(value = "店铺id") @Valid @RequestParam(value = "shopId", required = false) Long shopId, @ApiParam(value = "用户id") @Valid @RequestParam(value = "userId", required = false) Long userId) {
        if (shopId == null) {
            shopId = getShopId();
        }
        try {
            IPage<Map<String, Object>> page = shopUserStatisticsService.selectPageStatistics(pageStart, pageSize, shopId, userId, skuId, type, sTime, eTime);
            List<StatisticsListVo> list = page.getRecords().stream().map(map ->
                    new StatisticsListVo().num((Long) map.get("num"))
                            .date((String) map.get("days"))
            ).collect(Collectors.toList());
            return ResultGenerator.genSuccessResult(new PageInfo<StatisticsListVo>(list, (Page) page));
        } catch (Exception e) {
            log.error("统计列表，shopid--{}--pageStart--{}--pageSize--{}--userId--{}--skuId--{}--type--{}--sTime--{}--eTime", shopId, pageStart, pageSize, userId, skuId, type, sTime, eTime, e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result<List> skuBroswerCount(String startDate, String endDate) {
        Result result = new Result();
        Long shopId = getShopId();

        List list = shopUserStatisticsMapper.skuBroswerCount(shopId, startDate, endDate);
        result.setBizCode(ResultCode.CODE_SUCCESS);
        result.setData(list);

        return result;
    }

    @Override
    public Result<List> skuSellCount(String startDate, String endDate) {
        Result result = new Result();
        Long shopId = getShopId();

        List list = shopUserStatisticsMapper.skuSellCount(shopId, startDate, endDate);
        result.setBizCode(ResultCode.CODE_SUCCESS);
        result.setData(list);

        return result;
    }

    @Override
    public Result<List> skuSellCountDetail(String startDate, String endDate) {
        Result result = new Result();
        Long shopId = getShopId();

        List list = shopUserStatisticsMapper.skuSellCountDetail(shopId, startDate, endDate);
        result.setBizCode(ResultCode.CODE_SUCCESS);
        result.setData(list);

        return result;
    }

}
